package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Slf4j
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void addInterceptors (InterceptorRegistry registry){
           log.debug("将 拦截器iLoginInterceptor 注册到  WebMVC 中");
           log.debug("注册拦截器 RefreshTokenInterceptor 到 spring， 拦截所有请求， 用于刷新token在redis的过期时间 和 保存 token从redis获取到的用户信息到 ThreadLocal");
           registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);  //默认不加addPathPatterns 就是拦截所有请求
           log.debug("注册拦截器 LoginInterceptor 到spring， 拦截需要用户登录后才可以访问的页面,  通过ThreadLocal判断是否存有用户信息来 判断是否登录，是否放行 ");
           registry.addInterceptor(new LoginInterceptor())
                   .excludePathPatterns(
                           "/user/login" ,
                           "/user/code",
                           "/shop/**",
                           "/blog/hot",
                           "/shop-type/**",
                           "/upload/**",
                           "/voucher/**",
                           "/voucher-order/**").order(1);

    }

}
